package org.activiti.solon.config;

import org.flowable.engine.*;
import org.noear.solon.annotation.*;

import javax.sql.DataSource;

@Configuration
@Component(index = 20)
@Condition(onBean=DataSource.class)
public class ActivitiConfig {

    @Bean
    public ProcessEngine createEngine(@Inject("master") DataSource dataSource) {
        ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        cfg.setDataSource(dataSource);
        // 在构建过程引擎时，执行检查并在必要时执行模式的更新. 如果没有表则创建，
        cfg.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        return cfg.buildProcessEngine();
    }

    @Bean
    public RepositoryService createRepositoryService(@Inject ProcessEngine engine) {
        return engine.getRepositoryService();
    }

    @Bean
    public RuntimeService createRuntimeService(@Inject ProcessEngine engine) {
        return engine.getRuntimeService();
    }

    @Bean
    public TaskService createTaskService(@Inject ProcessEngine engine) {
        return engine.getTaskService();
    }

    @Bean
    public HistoryService createHistoryService(@Inject ProcessEngine engine) {
        return engine.getHistoryService();
    }

    @Bean
    public ManagementService createManagementService(@Inject ProcessEngine engine) {
        return engine.getManagementService();
    }
}
